package ph.loong.deliveryman.http;

import android.util.Log;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import ph.loong.deliveryman.app.MyApplication;
import ph.loong.deliveryman.utils.SPUtil;

/**
 * Author jc.lu.
 * Dates 2018/2/22.
 * Email Tbsupland@163.com.
 * desc
 */

public class AddCookiesInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();


        builder.addHeader("authorization", SPUtil.getString("token"));
        HashSet<String> preferences = (HashSet) MyApplication.Companion.getContext().getSharedPreferences("config",
                MyApplication.Companion.getContext().MODE_PRIVATE).getStringSet("cookie", null);
        if (preferences != null) {
            for (String cookie : preferences) {
                builder.addHeader("Cookie", cookie);
                Log.v("OkHttp", "Adding Header: " + cookie);
            }
        }
        return chain.proceed(builder.build());
    }
}
